/**
 * 
 */
package cn.ad.server.web.servlet.v2.parser;

import cn.ad.server.common.utils.IPUtils;
import cn.ad.server.web.spring.model.AvidRequest;
import cn.ad.server.web.spring.model.IAdserverRequest;
import cn.ad.server.web.spring.model.IAdserverResponse;
import cn.ad.server.web.spring.model.dactive.DactiveAdResponse;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Component;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 激活回调请求解析类
 * @author wangwei
 */

@Component("parser-avid")
public class AvidRequestParser extends HttpServletParserAdapter {
	
	
	/**
	 * 
	 */
	private static final String PARAM_MAC = "mac";
	/**
	 * 
	 */
	private static final String PARAM_MACORI = "macori";
	/**
	 * 
	 */
	private static final String PARAM_IDFA = "idfa";
	/**
	 * 
	 */
	private static final String PARAM_IDFAORI = "idfaori";
	/**
	 * 
	 */
	private static final String PARAM_IMEI = "imei";
	/**
	 * 
	 */
	private static final String PARAM_IMEIORI = "imeiori";
	/**
	 * 
	 */
	private static final String PARAM_UID = "uid";
	/**
	 * 
	 */
	private static final String PARAM_APPID = "appid";
	/**
	 * 广告位标识
	 */
	private static final String PARAM_CON_TYPE = "con_type";
	/**
	 * 订单标识
	 */
	private static final String PARAM_CON_VALUE = "con_value";
	/**
	 * 
	 */
	private static final String PARAM_CONV_TIME = "conv_time";
	/**
	 * 
	 */
	private static final String PARAM_CLIENT_IP = "client_ip";
	
	
	
	
	/* (non-Javadoc)
	 * @see cn.falconnect.adserver.adapter.HttpServletParser#parse(javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public IAdserverRequest parse(HttpServletRequest request) throws AdRequestParseException {
		AvidRequest adRquest = new AvidRequest();
		adRquest.setIp(IPUtils.parseIp(request));
		adRquest.setUa(request.getHeader("user-agent"));
		readRequestParam(request, adRquest);
		return adRquest;
	}
	
	/**
	 * 读取appflyer激活回调参数
	 * @param request
	 * @param appflyerRequest
	 * @throws AdRequestParseException
	 */
	public void readRequestParam(HttpServletRequest request, AvidRequest avidRequest)  throws AdRequestParseException{
		if(avidRequest == null){
			throw new NullPointerException("the argument AvidRequest is null");
		}
		avidRequest.setMac(request.getParameter(PARAM_MAC));
		avidRequest.setMacori(request.getParameter(PARAM_MACORI));
		avidRequest.setIdfa(request.getParameter(PARAM_IDFA));
		avidRequest.setIdfaori(request.getParameter(PARAM_IDFAORI));
		avidRequest.setImei(request.getParameter(PARAM_IMEI));
		avidRequest.setImeiori(request.getParameter(PARAM_IMEIORI));
		avidRequest.setUid(request.getParameter(PARAM_UID));
		avidRequest.setAppid(request.getParameter(PARAM_APPID));
		avidRequest.setCon_type(request.getParameter(PARAM_CON_TYPE));
		avidRequest.setCon_value(request.getParameter(PARAM_CON_VALUE));
		avidRequest.setConv_time(request.getParameter(PARAM_CONV_TIME));
		avidRequest.setClient_ip(request.getParameter(PARAM_CLIENT_IP));

	}
	protected final ObjectMapper objectMapper = new ObjectMapper();
	
	
	
	@Override
	public void writeResponse(IAdserverRequest adRequest, IAdserverResponse adResponse, HttpServletRequest request,
			HttpServletResponse response) throws AdRequestParseException {
		try {
			DactiveAdResponse resp = (DactiveAdResponse)adResponse;
			response.setContentType("application/json;charset=utf-8");
			ServletOutputStream outputStream = response.getOutputStream();
			objectMapper.writeValue(outputStream, resp.getAdReponse());
		} catch (JsonGenerationException e) {
			throw new AdRequestParseException(e.getCause());
		} catch (JsonMappingException e) {
			throw new AdRequestParseException(e.getCause());
		} catch (IOException e) {
			throw new AdRequestParseException(e.getCause());
		}
	}


}
